<?
$rightCol = 1;
$eventMenu = 1;
$step = 1;

include('includes/appInit.php');
$errorDivDisplay = 'none';
$eventID = $_GET['eventID'];

$events  = new Events();
$disData = new getData();

if ($_SESSION['logged'] == 1) {
	$email = $_SESSION['email'];
	if ($events->checkRegistration($_SESSION['memberID'],$_GET['eventID'])) {
		$error = 1;
		$errorMessage = 'You have already registered for this event.';
		$step = 0;
	} else {
		$step = 2;
	}	
}

if ($_GET['checkEmail'] == 1) {
	$eventID = $_POST['eventID'];
	$orgFlag = $_POST['orgFlag'];
	$email   = $_POST['email'];
	/*
	 * Check for valid email address
	 */
	$emailValidator = new Zend_Validate_EmailAddress();
	if (empty($email) || !$emailValidator->isValid($email)) {
		$error = 1;
		$errorMessage = 'The email address '.$email.' appears to be invalid.';
		$errorDivDisplay = '';
	}
	if ($error != 1) {
		$access  = new accountAccess();
		/*
		 * Check for existing email address
		 * If found, log user in
		 */
		$access->checkLogin($email);
		
		/*
		 * Are we registered for this event already?
		 */
		if ($events->checkRegistration($_SESSION['memberID'],$eventID)) {
			$error = 1;
			$errorMessage = 'You have already registered for this event.';
			$step = 0;
		} else {
			$step = 2;
		}
	}	
}

if ($_GET['register'] == 1) {
	$nameFirst = fieldToDB($_POST['nameFirst']);
	$nameLast  = fieldToDB($_POST['nameLast']);
	$address   = fieldToDB($_POST['address']);
	$city      = fieldToDB($_POST['city']);
	$state     = $_POST['state'];
	$zip       = fieldToDB($_POST['zip']);
	$email     = fieldToDB($_POST['email']);
	$phone     = fieldToDB($_POST['phone']);
	$iAm       = $_POST['iAm'];
	$childAgeYr= $_POST['childAgeYr'];
	$childAgeMo= $_POST['childAgeMonth'];
	$disability= $_POST['disability'];
	$proTitle  = fieldToDB($_POST['proTitle']);
	$proOrg    = fieldToDB($_POST['proOrg']);
	$eventID   = $_POST['eventID'];	
	$memberID  = $_POST['memberID'];	
	$eventTitle= $_POST['eventTitle'];
	$orgFlag   = $_POST['orgFlag'];
		
	$contactSubject = 'Event Registration: '.$eventTitle;
	
	if ($iAm == 'Parent') {
		$memberType = '|Parent|||';
	} elseif ($iAm == 'Professional') {
		$memberType = '||Professional||';
	}
	
	$childAge = $childAgeYr.'|'.$childAgeMo;
	
	/*
	 * Do some server side validation
	 */
	if (empty($nameFirst)) {
		$error = 1;
		$errorMessage = 'You must enter your first name.';
	}
	if (empty($nameLast)) {
		$error = 1;
		$errorMessage = 'You must enter your last name.';
	}
	if (empty($address)) {
		$error = 1;
		$errorMessage = 'You must enter your mailing address.';
	}	
	if (empty($city)) {
		$error = 1;
		$errorMessage = 'You must enter your city.';
	}
	if (empty($email)) {
		$error = 1;
		$errorMessage = 'You must enter your email address.';
	}
	
	if ($orgFlag == 'Swindells') {
		$orgField = 'OrgSwindells';
	} else {
		$orgField = 'OrgOrPTI';
	}
	
	if ($error != 1) {
		/*
		 * Save member information to database
		 */
		if (empty($memberID)) {
			$sql = "INSERT into cdc_ContactClientDemographics (
					FirstName,
					LastName,
					Address,
					City,
					State,
					Zip,
					Email1,
					Phone1,
					MemberType,
					ChildAge,
					OrganizationName,
					OrganizationTitle,
					$orgField,
					DateUpdate,
					Userid
					) values (
					'$nameFirst',
					'$nameLast',
					'$address',
					'$city',
					'$state',
					'$zip',
					'$email',
					'$phone',
					'$memberType',
					'$childAge',
					'$proOrg',
					'$proTitle',	
					'1',					
					'".date('Y-m-d')."',
					'1000'
					)";
			$result = mysql_db_query($dbname,$sql);
			$memberID = mysql_insert_id();
		} else {
			$sql = "UPDATE cdc_ContactClientDemographics SET
					FirstName         = '$nameFirst',
					LastName          = '$nameLast',
					Address           = '$address',
					City              = '$city',
					State             = '$state',
					Zip               = '$zip',
					Email1            = '$email',
					Phone1            = '$phone',
					MemberType        = '$memberType',
					ChildAge          = '$childAge',
					OrganizationName  = '$proOrg',
					OrganizationTitle = '$proTitle',
					$orgField         = '1',
					DateUpdate        = '".date('Y-m-d')."'
					WHERE
					MemberID = '$memberID'";
			$result = mysql_db_query($dbname,$sql);
		}
		
		/*
		 * Enter contact information and registration information
		 */
		/*
		 * Get last Contact ID Number (IDNumber)
		 */
		$sql = "SELECT MAX(IDNumber) AS MaxID from cdc_Contacts WHERE OrgFlag = '$orgFlag'";
		$result = mysql_db_query($dbname,$sql);
		$row = mysql_fetch_assoc($result);
		$maxContactID = $row['MaxID'];
		$newContactID = $maxContactID+1;
		
		$sql = "INSERT into cdc_Contacts (
				IDNumber,
				DateofContact,
				SubjectLine,
				ContactTypeID,
				Status,
				OrgFlag,
				DateUpdate,
				UseridEntry
				) values (
				'$newContactID',
				'".date('Y-m-d G:i:s')."',
				'$contactSubject',
				'666',
				'1',
				'$orgFlag',
				'".date('Y-m-d')."',
				'1000'
				)";
		$result = mysql_db_query($dbname,$sql);
		$contactID = mysql_insert_id();
		
		/*
		 * Link event with member
		 */
		
		$sql = "INSERT into cdc_linkEventMember (
				EventID,
				MemberID,
				DateRegister
				) values (
				'$eventID',
				'$memberID',
				'".date('Y-m-d G:i:s')."'
				)";
		$result = mysql_db_query($dbname,$sql);
		
		/*
		 * Link contact with member
		 */
		$sql = "INSERT into cdc_linkContactMember (
				ContactID,
				MemberID
				) values (
				'$contactID',
				'$memberID'
				)";
		$result = mysql_db_query($dbname,$sql);
		
		
		/*
		 * Send confirmation email
		 */
	}
}

if (!empty($_SESSION['memberID'])) {
	$memberID = $_SESSION['memberID'];
	$memberData = new accountInfo();
	$memberArray = $memberData->getMemberData($memberID);
	$nameFirst = $memberArray['FirstName'];
	$nameLast  = $memberArray['LastName'];
	$address   = DBToField($memberArray['Address']);
	$city      = $memberArray['City'];
	$state     = $memberArray['State'];
	$zip       = $memberArray['Zip'];
	$email     = $memberArray['Email1'];
	$phone     = $memberArray['Phone1'];
	$memberType= $memberArray['MemberType'];
	$childAge  = $memberArray['ChildAge'];
	$orgName   = $memberArray['OrganizationName'];
	$orgTitle  = $memberArray['OrganizationTitle'];	
}	

/*
 * Turn on error message?
 */
if ($error == 1) {
	$errorDivDisplay = '';
}

/*
 * Get some event information
 */
if (empty($eventID) || !is_numeric($eventID)) {
	header('Location: events.php');
}
$eventArray = $events->getEventDetails($eventID,'array');
include('includes/header.php');
include('includes/registrationFormJavaScript.php');
?>
	<div id="subPage">
		<div class="leftCol">
			<div id="subPageContent" class="flex">
			<h1><?=$translate->_('Event Registration'); ?></h1>
				<p>
					<div style="float: left; width: 50%;"><h2><?=$eventArray['EventName']; ?></h2></div>
					<div style="float: right; width: 50%; text-align: right;"><h3 style="text-align: right;"><?=$eventArray['DateString']; ?></h3></div>
					<div style="clear: both; height: 10px;"></div>
					
					<p>This is some explanatory text for the event registration form.</p>
					
					<div style="height: 20px;"></div>
					<? include('includes/registrationForm.php'); ?>	
				</p>
			</div>
			<div style="height: 200px;"></div>
		</div>
		<div class="rightCol">
			<div class="rightColInner flex">
				<? include('includes/rightColumn.php'); ?>
			</div>
		</div>
		<div style="clear: both;"></div>	
	</div>	
<?
include('includes/footer.php');
?>